<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 手写了一个bind方法
      Function.prototype.myBind = function (obj, ...args1) {
        // 缓存调用bind的函数
        const self = this; // 嵌套一层函数，返回函数调用apply方法的结果 // 此处也可以补充传参

        return function (...args2) {
          return self.apply(obj, [...args1, ...args2]);
        };
      };

      function fn(greeting1) {
        console.log(greeting1 + ", " + this.name + "!");
      }

      let obj1 = { name: "zs1" };
      let obj2 = { name: "zs2" };
      let obj3 = { name: "zs3" };
      let fn1 = fn.myBind(obj1, "hi"); /* 
            fn1=function(...arg2){
                return fn.apply(obj1,['hi'])
            } 
          */
      let fn2 = fn1.myBind(obj2, "hello"); /* 
            fn2=function(...arg2){
                return fn1.apply(obj2,['hi',hello'])
            }
          */
      fn2.apply(obj3, ["111111111111"]);
    </script>
  </body>
</html>
